<?php

namespace App\Console\Commands;

use App\Enum\CodeEnum;
use App\Enum\TravelEnum;
use App\Models\DriverModel;
use App\Services\QrcodeService;
use Illuminate\Console\Command;

class DriverQrcodeGenerateCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'qrcode:driver';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '生成司机的qrcode';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {

        $drivers =  DriverModel::query()->whereNull('qrcode_url')->select('id')->limit(3)->get()->toArray();
        if( !empty($drivers) ){
            try {
                foreach ($drivers as $driver) {

                    $userId = $driver['id'];
                    $url = sprintf(TravelEnum::QRCODE_INFO_URL[TravelEnum::QRCODE_TYPE_DRIVER_INFO], env('BASE_URL'), $userId);

                    $driverQrcodeUrl = QrcodeService::getQrcode($url, $userId, 'qrcode');
                    DriverModel::query()->where(['id' => $userId])->update(['qrcode_url' => $driverQrcodeUrl]);
                    $this->info('generate qrcode for driver_id'.$userId);
                }
            } catch (\Exception $e) {
                $this->info($e->getMessage());
                return 0;
            }
        }

        return 0;
    }
}
